/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package modele;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author sio
 */
public class DAO {
    private static final String url = "http://gaemedecins.appspot.com/Controleur/medParDep/listeDep";
    private static final String urlmed = "http://gaemedecins.appspot.com/Controleur/medParDep/listeMed/";
    public static List<String> getLesDeps() throws ParserConfigurationException, SAXException, IOException {
        List<String> lesDeps = new ArrayList<String>();
        URL myURL = new URL(url);
        Document doc;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(myURL.openStream());
        Element racine = (Element) doc.getDocumentElement();
        
        NodeList listeMed = racine.getElementsByTagName("Departement");
        for (int i = 0; i < listeMed.getLength(); i++) {
            Node medecin = listeMed.item(i);
            NodeList lesProprietes = medecin.getChildNodes();
            for (int j = 0; j < lesProprietes.getLength(); j++) {
                if(lesProprietes.item(j).getNodeName().equals("num")) {
                    lesDeps.add(lesProprietes.item(j).getTextContent().trim());
                    break;
                }
            }
        }
        return lesDeps;
    } 
    public static List<Medecin> getLesMeds(String departement) throws ParserConfigurationException, SAXException, IOException {
        List<Medecin> lesMeds = new ArrayList<Medecin>();
        URL myURL = new URL(urlmed+departement);
        Document doc;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(myURL.openStream());
        Element racine = (Element) doc.getDocumentElement();
        
        NodeList listeMed = racine.getElementsByTagName("Medecin");
        for (int i = 0; i < listeMed.getLength(); i++) {
            Node medecin = listeMed.item(i);
            NodeList lesProprietes = medecin.getChildNodes();
            String nom = null;
            String prenom = null;
            String adresse = null;
            String specialite = null;
            String tel = null;
            for (int j = 0; j < lesProprietes.getLength(); j++) {
                if(lesProprietes.item(j).getNodeName().equals("nom")) {
                    nom = lesProprietes.item(j).getTextContent().trim();
                }
                if(lesProprietes.item(j).getNodeName().equals("prenom")) {
                    prenom = lesProprietes.item(j).getTextContent().trim();
                }
                if(lesProprietes.item(j).getNodeName().equals("adresse")) {
                    adresse = lesProprietes.item(j).getTextContent().trim();
                }
                if(lesProprietes.item(j).getNodeName().equals("specialite")) {
                    specialite = lesProprietes.item(j).getTextContent().trim();
                }
                if(lesProprietes.item(j).getNodeName().equals("tel")) {
                    tel = lesProprietes.item(j).getTextContent().trim();
                }
            }
            Medecin med = new Medecin(nom, prenom, adresse, specialite, tel);
            lesMeds.add(med);
        }
        return lesMeds;
    } 
}
